package com.yjd.comm.util;

import com.yjd.comm.repayment.dto.OverdueDto;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.vo.Constant;

import java.math.BigDecimal;

/**
 * Created by yjd-wap on 2017/9/21.
 */
public class RepaymentUtils {

    /**
     *  逾期罚息计算
     * @param price   应还本金
     * @param repaymentTime 应还款日期
     * @param actualTime 时间还款日期
     * @return
     * @throws Exception
     */
    public static OverdueDto overdueInterest(Double price, Long repaymentTime, Long actualTime)throws Exception{
        if(price==null||price==0){
            FrameUtil.throwBaseException1("应还本金参数异常");
        }

        if(repaymentTime==null||repaymentTime==0){
            FrameUtil.throwBaseException1("应还款日期参数异常");
        }

        if(actualTime==null||actualTime==0){
            FrameUtil.throwBaseException1("实还款日期参数异常");
        }

        if (repaymentTime >= actualTime) {
            return new OverdueDto(price,repaymentTime,actualTime,0,0D);
        }
        //逾期天数
        Long  overdueTimes = actualTime - repaymentTime;
        BigDecimal overdueDecimal = new BigDecimal(overdueTimes);
        BigDecimal timesDecimal = new BigDecimal(86400);
        Integer overdueDay = overdueDecimal.divide(timesDecimal, 0, BigDecimal.ROUND_CEILING).intValue();
        Double overduePrice = FeeCalcUtil.round(ArithUtil.muls(price,getOverdueFinePriceFee(overdueDay),overdueDay, 0.01));
        return  new OverdueDto(price,repaymentTime,actualTime,overdueDay,overduePrice);
    }

    /**
     * 根据逾期天数获取逾期罚息费率(百分比)
     *
     * @param overdueDay
     *            逾期天数
     * @return
     * @throws Exception
     */
    public static Double getOverdueFinePriceFee(Integer overdueDay)
            throws Exception {
        return SysParamUtil.getParamValueByRegIn(Constant.SysParamKeyEnum.OVERDUE_FINE_PRICE_FEE,
                Double.valueOf(overdueDay.toString()));
    }
}
